package com.letv.android.client.parser;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.letv.core.bean.ChannelListBean;
import com.letv.core.bean.ChannelListBean.Channel;
import com.letv.core.bean.SiftKVP;
import com.letv.core.parser.LetvMobileParser;

/**
 * 频道列表解析
 * 
 * */
public class ChannelsParser extends LetvMobileParser<ChannelListBean> {

    /**
     * 频道数据节点
     * */
    private final String CHANNEL = "channel";

    private final String MZCID = "mzcid";
    /**
     * 频道名称
     * */
    private final String NAME = "name";
    /**
     * 频道图标
     * */
    private final String ICON = "pic1";
    /**
     * 频道图标选中
     */
    private final String ICON_SELECTED = "pic2";
    /**
     * 频道类型：1-专辑、2-视频、3-精品推荐
     * */
    private final String TYPE = "type";
    /**
     * 关键字、看点
     * */
    private final String SUBTITLE = "subtitle";
    /**
     * html5地址
     */
    private final String HTMLURL = "url";

    /**
     * 刷选条件
     */
    private final String FILTER = "filter";

    /**
     * 进入频道id条件
     */
    private final String PAGEID = "pageid";

    /**
     * 分类标题
     */
    private final String TITLE = "title";
    /**
     * 数据列表
     */
    private final String DATA = "data";
    private final String ITEM_KEY = "key";
    private final String ITEM_ID = "id";
    private final String ITEM_VALUE = "value";
    private final String INTEREST = "趣味";

    @Override
    public ChannelListBean parse(JSONObject data) throws JSONException {

        ChannelListBean channelList = new ChannelListBean();
        JSONArray array = data.optJSONArray(CHANNEL);
        if (array != null) {
            Channel channel = null;
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.optJSONObject(i);
                if (object != null) {
                    String title = object.optString(TITLE);
                    JSONArray itemArray = object.optJSONArray(DATA);
                    if (itemArray != null) {
                        ArrayList<Channel> list = new ArrayList<ChannelListBean.Channel>();
                        for (int j = 0; j < itemArray.length(); j++) {
                            channel = parseItem(itemArray.optJSONObject(j));
                            if (channel != null) {
                                if (channel.getType() == Channel.CHANNEL_TYPE_VIDEOS
                                        || channel.getType() == Channel.CHANNEL_TYPE_ALBUMS) {
                                    if (channel.getId() > 0
                                            || !TextUtils.isEmpty(channel.getHtmlUrl())
                                            || (!TextUtils.isEmpty(channel.getName()) && channel.getName().equals(
                                                    INTEREST))) {
                                        list.add(channel);
                                    }
                                }
                            }
                        }
                        // 只有标题，没有列表数据时，该标题不显示
                        if (list.size() > 0) {
                            channelList.getTitleList().add(title);
                            channelList.getChannelMap().put(title, list);
                        }
                    }
                }
            }
        }
        return channelList;
    }

    private Channel parseItem(JSONObject object) {
        Channel channel = new Channel();
        try {
            channel.setId(object.optInt(MZCID));
            channel.setName(object.optString(NAME));
            channel.setIcon(object.optString(ICON));
            channel.setSelectedIcon(object.optString(ICON_SELECTED));
            channel.setType(object.optInt(TYPE));
            channel.setSubtitle(object.optString(SUBTITLE));
            channel.setHtmlUrl(object.optString(HTMLURL));
            channel.setPageid(object.optString(PAGEID));
            if (object.has(FILTER)) {
                ArrayList<SiftKVP> filterList = null;
                JSONArray filterArray = object.optJSONArray(FILTER);
                if (filterArray != null && filterArray.length() > 0) {
                    JSONObject jsonData = null;
                    SiftKVP newSiftKVP = null;
                    filterList = new ArrayList<SiftKVP>();
                    for (int ii = 0; ii < filterArray.length(); ii++) {
                        jsonData = filterArray.optJSONObject(ii);
                        if (jsonData != null) {
                            newSiftKVP = new SiftKVP();
                            newSiftKVP.setFilterKey(jsonData.optString(ITEM_KEY));
                            newSiftKVP.setId(jsonData.optString(ITEM_ID));
                            newSiftKVP.setKey(jsonData.optString(ITEM_VALUE));
                            filterList.add(newSiftKVP);
                        }
                    }
                    channel.setChannelIdInfo(filterList);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return channel;
    }
}
